package com.wutuo.aggragation.rollcall.dao;

import cn.apestar.core.utils.ListUtils;
import com.wutuo.aggragation.rollcall.domain.TaskDetail;
import com.wutuo.child.provider.enums.RollCallState;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class TaskDetailSqlProvider {

    public static final String TASKDETAIL_COLUMNS = "id, task_id as taskId, user_id as userId, state, create_by as createBy, create_date as createDate, update_by as updateBy, update_date as updateDate, remarks, del_flag  as delFlag";

    public String insertSelective(TaskDetail record) {
        BEGIN();
        INSERT_INTO("task_detail");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getTaskId() != null) {
            VALUES("task_id", "#{taskId,jdbcType=BIGINT}");
        }

        if (record.getUserId() != null) {
            VALUES("user_id", "#{userId,jdbcType=BIGINT}");
        }

        if (record.getState() != null) {
            VALUES("state", "#{state,jdbcType=VARCHAR}");
        }

        if (record.getCreateBy() != null) {
            VALUES("create_by", "#{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            VALUES("create_date", "#{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            VALUES("update_by", "#{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            VALUES("update_date", "#{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            VALUES("remarks", "#{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            VALUES("del_flag", "#{delFlag,jdbcType=CHAR}");
        }

        return SQL();
    }

    public String updateByPrimaryKeySelective(TaskDetail record) {
        BEGIN();
        UPDATE("task_detail");

        if (record.getTaskId() != null) {
            SET("task_id = #{taskId,jdbcType=BIGINT}");
        }

        if (record.getUserId() != null) {
            SET("user_id = #{userId,jdbcType=BIGINT}");
        }

        if (record.getState() != null) {
            SET("state = #{state,jdbcType=VARCHAR}");
        }

        if (record.getCreateBy() != null) {
            SET("create_by = #{createBy,jdbcType=BIGINT}");
        }

        if (record.getCreateDate() != null) {
            SET("create_date = #{createDate,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateBy() != null) {
            SET("update_by = #{updateBy,jdbcType=BIGINT}");
        }

        if (record.getUpdateDate() != null) {
            SET("update_date = #{updateDate,jdbcType=TIMESTAMP}");
        }

        if (record.getRemarks() != null) {
            SET("remarks = #{remarks,jdbcType=VARCHAR}");
        }

        if (record.getDelFlag() != null) {
            SET("del_flag = #{delFlag,jdbcType=CHAR}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }

    public String selectByChildIds(Map<String, Object> param) {
        BEGIN();
        SELECT(TASKDETAIL_COLUMNS);
        FROM("task_detail t");
        WHERE("del_flag = '1'");
        if (param.get("childIds") != null) {
            List<Long> childIds = (List<Long>) param.get("childIds");
            if (CollectionUtils.isNotEmpty(childIds)) {
                String ids = ListUtils.listToString(childIds, ',');
                WHERE(" t.user_id in (" + ids + ")");
            }
        }

        if (param.get("teacherId") != null) {
            WHERE(" t.create_by =  #{teacher_id,jdbcType=BIGINT}");
        }

        if (param.get("taskId") != null) {
            WHERE(" t.task_id = #{taskId,jdbcType=BIGINT}");
        }

        ORDER_BY("t.create_date desc");

        // if (param.get("state") != null) {
        // WHERE(" t.state = #{state,jdbcType=VARCHAR}");
        // }
        return SQL();
    }

    public String selectByChildIdAndTaskId(Map<String, Object> param) {
        BEGIN();
        SELECT(TASKDETAIL_COLUMNS);
        FROM("task_detail t");
        WHERE("del_flag = '1'");
        if (param.get("childId") != null) {
            WHERE(" t.user_id = #{childId,jdbcType=BIGINT}");

        }

        if (param.get("taskId") != null) {
            WHERE(" t.task_id = #{taskId,jdbcType=BIGINT}");
        }
        return SQL();
    }

    public String countSignedChildren(Map<String, Object> param) {
        BEGIN();
        SELECT("count(1)");
        FROM("task_detail t");
        WHERE("del_flag = '1'");
        if (param.get("teacherId") != null) {
            WHERE(" t.create_by =  #{teacher_id,jdbcType=BIGINT}");
        }

        if (param.get("taskId") != null) {
            WHERE(" t.task_id = #{taskId,jdbcType=BIGINT}");
        }

        if (param.get("state") != null) {
            if (RollCallState.SIGNED.getValue().equals(param.get("state"))) {
                WHERE(" t.state in ('intime','late')");
            } else {
                WHERE(" t.state = #{state,jdbcType=VARCHAR}");
            }
        }

        return SQL();
    }

    public String listChildIdsByState(Map<String, Object> param) {
        BEGIN();
        SELECT("user_id");
        FROM("task_detail t");
        WHERE("del_flag = '1'");

        RollCallState state = (RollCallState) param.get("state");

        if (state == null) {
            return SQL();
        }

        if (RollCallState.SIGNED.equals(state)) {
            WHERE(" t.state in ('intime','late')");
        } else {
            WHERE(" t.state = #{state,jdbcType=VARCHAR}");
        }

        if (param.get("teacherId") != null) {
            WHERE(" t.create_by =  #{teacher_id,jdbcType=BIGINT}");
        }

        if (param.get("taskId") != null) {
            WHERE(" t.task_id = #{taskId,jdbcType=BIGINT}");
        }
        return SQL();
    }
}
